package cn.shyouchi.wuguanjia.android.ui.meetting.common;

import android.view.View;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.impl.OnItemClickListener;
import cn.shyouchi.wuguanjia.android.ui.event.arrange.PersonRankAdapter;

/**
 * Created by Eenie on 2017/2/8 at 15:04
 * Email: 472279981@qq.com
 * Des:
 */

public class SelMeetPersonAdapter extends BaseQuickAdapter<MeetPerson, BaseViewHolder> {


    OnItemClickListener mClickListener;

    public SelMeetPersonAdapter(List<MeetPerson> data, OnItemClickListener<MeetPerson> itemClickListener) {

        super(R.layout.item_sel_meet_person_layout, data);
        mClickListener = itemClickListener;
    }

    @Override
    protected void convert(final BaseViewHolder holder, final MeetPerson person) {
        holder.setText(R.id.ck_person_name, person.getName());
        CheckBox c = holder.getView(R.id.ck_person_name);
        c.setChecked(person.isCheck());
        c.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickListener.onItemClick(person);
                person.setCheck(!person.isCheck());

                if (isSelAll()) {
                    mClickListener.onDel(new PersonRankAdapter.Person(0, "", null));

                } else {
                    mClickListener.onDel(new PersonRankAdapter.Person(1, "", null));
                }

                notifyItemChanged(holder.getAdapterPosition());


            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickListener.onItemClick(person);
                person.setCheck(!person.isCheck());
                if (isSelAll()) {
                    mClickListener.onDel(new PersonRankAdapter.Person(0, "", null));

                } else {
                    mClickListener.onDel(new PersonRankAdapter.Person(1, "", null));
                }
                notifyItemChanged(holder.getAdapterPosition());
            }
        });




    }


    private boolean isSelAll() {
        for (MeetPerson p : getData()) {
            if (!p.isCheck()) {
                return false;
            }
        }
        return true;
    }


    public   ArrayList<MeetPerson>  getSelPerson() {
        ArrayList<MeetPerson> meetPersons = new ArrayList<>();
        for (MeetPerson p : getData()) {
            if (p.isCheck()) {
                meetPersons.add(p);
            }
        }
        return meetPersons;
    }


}
